package cn.edu.gcu.lost.sys.controller;

import cn.edu.gcu.lost.entity.User;
import cn.edu.gcu.lost.sys.util.HttpUtils;
import net.sf.json.JSONObject;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping(value = "")
public class SendMessageController {


//	@Resource(name="sendMessagSerice")
//	private ISendMessage messageService;

    @Value("${task.weiboPath}")
    private String weiboPath; // 讯通地址

    @Value("${task.basePath}")
    private String basePath; // 应用的ip和端口
//	

    @Value("${eid}")
    private String eid; // 工作圈id

    @Value("${appid}")
    private String appid; // 应用id

    @Value("${appsecret}")
    private String appsecret; // 应用秘钥

//	@Value("${task.pubId}")
//	private String pubId; // 公共号id

//	@Value("${task.pubKey}")
//	private String pubKey; // 公共号秘钥

	
	/*
	 * 
	/*
	 * 
	 
	  接口：demo   单点登陆接口
	   	  1.参数：(String)  ticket ----用户登陆ticket  	  
		  2.返回结果：     成功： 跳转到index.jsp页面
		  			        失败： 跳转到error.jsp页面
	 * 
	 * 
	 */


    @ResponseBody
    @RequestMapping(value = "/access")
    public ModelAndView demo(@RequestParam(value = "ticket", required = true) String ticket, HttpServletRequest
            request, HttpServletResponse response) {
        ModelAndView mv = new ModelAndView("/error"); //需要更改为错误页面
        if (ticket == null || ticket.equals("")) {
            return mv;
        }
        request.getSession().setAttribute("ticket", ticket);
        try {
            //获取token
            String url_token = weiboPath + "/openauth2/api/token?grant_type=client_credential&appid=" + appid +
                    "&secret=" + appsecret;
            String string_access_token = HttpUtils.post(url_token, "", "");
            JSONObject json_access_token = JSONObject.fromObject(string_access_token);
            String access_token = json_access_token.get("access_token").toString();
            //获取上下文
            String url = weiboPath + "/openauth2/api/getcontext?ticket=" + ticket + "&access_token=" + access_token;
            String string_persionInfo = HttpUtils.post(url, "", eid);
            if (string_persionInfo != null) {
                JSONObject json_persionInfo = JSONObject.fromObject(string_persionInfo);
                if (json_persionInfo != null) {
                    User user = new User();
                    String openid = json_persionInfo.get("openid").toString();
                    String username = json_persionInfo.get("username").toString();
                    String photoUrl = json_persionInfo.get("photoUrl").toString();
                    String mobile = json_persionInfo.get("mobile").toString();
                    user.setOpenid(openid);
                    user.setUsername(username);
                    user.setPhotoUrl(photoUrl);
                    user.setMobile(mobile);
                    request.getSession().setAttribute("user", user);
                    request.getSession().setAttribute("openid", user.getOpenid());
                    mv.addObject("openid", openid);//用户id
                    mv.setViewName("/index");
                    org.apache.shiro.subject.Subject subject = SecurityUtils.getSubject();
                    UsernamePasswordToken token = new UsernamePasswordToken("yun@#$%^&*()", "admin");
                    subject.login(token);
                    return mv;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return mv;


    }

}
